<template>
	<!-- <bar></bar> -->
<view style="height: 100vh;width:100%">
	<homeCustom v-if="shop_name">
		<block slot="content" class="text-white" style="height: 94%;">
			<view style="display: flex;flex-direction: column;justify-content: center;align-items: center; width: 100%; height: 100%;">
				<view v-if="shareName">
					{{shareName}}的
				</view>
				<view>
					{{shop_name}}
				</view>
			</view>
		</block>
	</homeCustom>
	<scroll-view scroll-y="true" >
		<swiper class="card-swiper bg-white square-dot" :indicator-dots="true" :circular="true"
			 :autoplay="true" interval="3000" duration="500" @change="cardSwiper" indicator-color="#8799a3"
			 indicator-active-color="#0081ff">
			<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''" @tap="handleSwiper(item)">
				<view class="swiper-item">
					<image :src="item.pic_url" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="bg-white padding">
			<view class="cu-list" v-for="(item,index) in brandList" :key="index" @tap="introduce">
				<view class="cu-item">
					<view class="bg-img flex align-center" :style="'background-image: url('+ item.pic_url+');height:120rpx;'">
					</view>
				</view>
			</view>
			<view class="cu-list " v-for="(item,index) in activityGoodList" :key="index" @tap="handleSwiper(item)">
				<view class="cu-item">
					<view class="bg-img flex align-center" :style="'background-image: url('+ item.pic_url+');min-height:270rpx;'">
					</view>
				</view>
			</view>
		</view>
		<view class="bg-white margin-top padding" style="padding-bottom: 80rpx;">
			<view class="cu-list" v-for="(item,index) in seriesList" :key="index" style="overflow: hidden;" @tap="handleActivityTheme(item,index)">
				<view class="cu-item">
					<view class="bg-img flex align-center" :style="'background-image: url('+ item.pic_url+');height:170rpx;'">
					</view>
				</view>
			</view>
		</view>
	</scroll-view>
</view>
</template>

<script>
	import GoodsCard from "../component/GoodsCard.vue"
	import * as Home_API from '@/api/home.js'
	import * as User_API from '@/api/user.js'
	import * as Goods_API from '@/api/goods.js'
	import util from "@/utils/uuid.js";
	let shop_id = 2
	export default {
		data() {
			return {
				goodsCategory:{
					name:'推荐专区'
				},
				// 导航条
				TabCur: '0',
				scrollLeft: 0,
				// 导航条end
				scrollTop:0,//屏幕位置
				TabCurTab:0,//吸附置顶的偏差值
				ceil_top:'',//导航条置顶高度
				 // 轮播图
				cardCur: 0,
				swiperList: [],
				productList: [],
				//显示异常屏幕回到初始化位置开关
				showTop:false,//异常
				shop_name:uni.getStorageSync('shop').shop_name || '',
				member_id:null,
				shop_id:null,
				activityGoodList:[], //权益商品
				brandList:[], //品牌
				seriesList:[], //系列商品
				shareName:''
			}
		},
		components:{
			GoodsCard
		},
		onLoad(e){
			this.shop_id = e.shop_id
			this.member_id = e.member_id
			if (e.q) {
			  let queryAll = decodeURIComponent(e.q);
			  let id = gup('shop_id', queryAll)
			  uni.setStorageSync('shop_id',id)
			}
			if(e.shop_id){
				this.shop_id = parseInt(e.shop_id) 
				uni.setStorageSync('shop_id',this.shop_id)
			}else{
				this.shop_id =2
				uni.setStorageSync('shop_id',this.shop_id)
			}
			if(e.shareUser){
				let shareUser = e.shareUser
				uni.setStorageSync('shareUser',{'shareU':e.shareUser})
				// console.log('e.shareUser',e.shareUser,shareUser)
				let shop = uni.getStorageSync('shop') || {}
				shop.shareU = e.shareUser
				uni.setStorageSync('shop',shop)
				// this.shareName = e.shareUser
				let role_id = uni.getStorageSync('role').member_role_id
				if(role_id==4 || role_id== 3 || role_id==2 || role_id==1){
					this.shareName = uni.getStorageSync('userInfo').nickname
				}else{
					// console.log('just call', e.shareUser+'==========='+this.shareName)
					if(this.shareName){
						if(this.shareName !=''){
							if(shareUser+'' !== this.shareName+''){
								this.shareName = shareUser
							}
						}else{
							this.shareName = shareUser
						} 
					}else{
						this.shareName = shareUser
					}
				}
			}
			if(e.shop_name){
				let shop = uni.getStorageSync('shop') || {}
				shop.shop_name = e.shop_name
				this.shop_name = e.shop_name
				uni.setStorageSync('shop',shop)
			}
			if(e.phone){
				uni.setStorageSync('referrer', e.phone)
			}
			// if(uni.getStorageSync('shareUser')){
			// 	console.log('e.StorageSync',uni.getStorageSync('shareUser'))
			// 	this.shareName = uni.getStorageSync('shareUser').shareU
			// }
			this.checkRole()
			this.GET_Swiper()
			this.GET_ShopDetail()
			this.GET_SeriesList()
			this.shop_name = uni.getStorageSync('shop').shop_name
		},
		watch:{
			shareName(){
				this.checkRole()
			}
		},
		onShow(){
			const uuid = wx.getStorageSync('uuid')
			if(!uuid){
				uni.setStorageSync('uuid',util.wxuuid())
			}
			setTimeout(()=>{
				this.checkRole()
			},1000)
		},
		//上拉刷新
		onPullDownRefresh() {
			setTimeout(()=> {
				uni.stopPullDownRefresh()
				this.GET_Swiper()
				this.GET_ShopDetail()
				this.GET_SeriesList()
				this.checkRole()
			}, 200);
		},
		//上拉刷新
		onReachBottom(){
		},
		methods: {
			onShareAppMessage() {
				let role_id = uni.getStorageSync('role').id
				let user = uni.getStorageSync('userInfo').nickname
				let phone = uni.getStorageSync('userInfo').mobile
				let title = ''
				let path = ''
				if(role_id){
					title = user +'的' +this.shop_name+',快进来看看吧'
					path = '/pages/home/home?shareUser='+user+'&shop_name='+this.shop_name+'&phone='+phone
				}else{
					title = this.shop_name+'欢迎您的光临'
					path = '/pages/home/home'
				}
			    return {
					title: title,
					path: path
			    }
			},
			checkRole(){
				let role_id = uni.getStorageSync('role').member_role_id
				if(role_id==4 || role_id== 3 || role_id==2 || role_id==1){
					this.shareName = uni.getStorageSync('userInfo').nickname
				}else{
					this.shareName = uni.getStorageSync('shareUser').shareU
				}
			},
			// 导航条点击
			tabSelect(e) {
			    this.TabCur = e.currentTarget.dataset.id
			},
			onPageScroll(e) {
				this.scrollTop = e.scrollTop
			    if (e.scrollTop > 500) {
					this.showTop = false;
			    } else {
					this.showTop = true;
			    }
			},
			goTop() {
				uni.pageScrollTo({
				    scrollTop: 0,
				    duration: 300
				})
			},
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			GET_ShopDetail(){
				let params = {}
				let shop_id = this.shop_id?this.shop_id:2
				if(this.member_id){
					params= {member_id:this.member_id}
				}
				Home_API.getShopDetail(shop_id,params).then(res =>{
					this.shop_name = res.shop_name
					if(res.shop_name){
						uni.setStorageSync('shop',{shop_name:res.shop_name})
					}
				})
			},
			/**
			 * 获取URL中某个字符串字段
			*/
			gup(name, url) {
			  if (!url) url = location.href;
			  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
			  var regexS = "[\\?&]" + name + "=([^&#]*)";
			  var regex = new RegExp(regexS);
			  var results = regex.exec(url);
			  return results == null ? null : results[1];
			},
			GET_ProductList(){
				Home_API.HomeGoodsList(shop_id).then(res =>{
					this.productList = res
				})
			},
			GET_Swiper(){
				Home_API.HomeSwiper(shop_id).then(res =>{
					let swiperList = []
					if(res.length){
						swiperList = res
						setTimeout(()=>{
							this.swiperList = swiperList
						},200)
					}
				})
			},
			introduce(){
				uni.navigateTo({
					url:'../introduce/index'
				})
			},
			// 轮播图点击事件
			handleSwiper(item){
				if(item.operation_type === "GOODS"){
					uni.navigateTo({
						url:"/pages/goods/goods?goods_id="+item.operation_param
					})
				}else if(item.operation_type === "URL"){
					uni.navigateTo({
						url:item.operation_param
					})
				}
			},
			GET_SeriesList(){
				let shop_id = this.shop_id?this.shop_id:2
				let that =this
				Goods_API.getseriesList(shop_id).then(res =>{
					let goodlist = []
					let brandList = []
					let seriesList = []
					if(res.length){
						res.map(item =>{
							if(item.operation_type === "GOODS"){
								goodlist.push(item)
							}
							if(item.operation_type === "BRAND"){
								brandList.push(item)
							}
							if(item.operation_type === "SERIES"){
								seriesList.push(item)
							}
						})
						setTimeout(()=>{
							this.activityGoodList = goodlist
							this.brandList = brandList
							this.seriesList = seriesList
						},200)
						console.log('',this.activityGoodList,this.seriesList,this.brandList)
					}
				})
			},
			handleActivityTheme(item,index){
				uni.reLaunch({
					url:'../category/category?active='+index+'&operation_param='+item.operation_param
				})
			}	
		}
	}
</script>

<style lang="scss" scoped>
	.toptitle{
		  position: -webkit-sticky;
		  position: sticky;
		  top: var(--window-top);
		  z-index: 99;
	}
	/* 点击回到顶部 */
	
	.goTop image {
		height: 60rpx;
		width: 60rpx;
		border-radius: 100%;
		position: fixed;
		bottom: 150rpx;
		right: 60rpx;
		z-index: 10000;
	}
	/* 撑高线条 */
	.lines{
	  display: flex;
	  width: 100%;
	  height: 150rpx;
	}
	/* end */
	
	/* 页脚 */
	.foot{
	  display: flex;
	  padding: 10rpx;
	  align-items: center;
	  justify-content:space-between;
	}
	.foot-1{
	  width: 35%;
	  height: 1rpx;
	  background: gainsboro;
	}
	/* end */
	
	/* 登陆按钮 */
	.loginButton{
	  width: 100%;
	}
	/* end */

</style>
